/*   
 * Project: simple-rpc-load-consistent-hash
 * FileName: ProducerSharedUtils.java
 * Company: Chengdu Sicent Technology Co.,Ltd
 * version: V1.0
 */
package org.simple.rpc.load.hash;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * Description:
 * @author: wangbo
 * @date: 2015年10月27日下午2:44:46
 */

public class ProducerSharedUtils implements Serializable {
	
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private static List<ProducerLoadInfo> shardsSource=new ArrayList<ProducerLoadInfo>();
	private static Sharded<String,ProducerLoadInfo> sharded;
	//默认权重 --- 这里涉及的主要是可能存在的 一个服务器要承载更多的任务，目前默认都为1 及所有服务器都保持一致
	private static Integer DEFAULT_WEIGHT=1;

	private static final Logger logger = LoggerFactory.getLogger(ProducerSharedUtils.class);
	
    public ProducerSharedUtils(List<String> serverUrls){
    	for (int i = 0; i < serverUrls.size(); i++) {
    		ProducerLoadInfo producerLoadInfo=new ProducerLoadInfo("server"+i, serverUrls.get(i), "v1.0.0", DEFAULT_WEIGHT);
    		shardsSource.add(producerLoadInfo);
		}
    	logger.info("init shared info===>>>>>>"+shardsSource.toString());
    	sharded=new Sharded<String,ProducerLoadInfo>(shardsSource);
    }
	
	public String getServerUrlInfo(String changeInfo){
		return sharded.getShard(changeInfo);
	}

}
